/**
 * Created by haoli on 3/13/14.
 */

var mysql = require('mysql');
var dataconf = require('../conf/dataconfige');
//var connection = mysql.createConnection({
//     host       : dataconf.host,
//     user       : dataconf.user,
//     password   : dataconf.password,
//     database   : dataconf.database,
//     prefix     : dataconf.prefix
//})

//connection.connect();

//module.exports = function(){
//    var connection
//
//    this.connect = function(){
//       connection =  mysql.createConnection({
//            host       : dataconf.host,
//            user       : dataconf.user,
//            password   : dataconf.password,
//            database   : dataconf.database,
//            prefix     : dataconf.prefix
//        });
//       connection.connect();
//    };
//
//    this.close = function(){
//        connection.end();
//    };
//};

//建立连接池
var pool = mysql.createPool({
     host       : dataconf.host,
     user       : dataconf.user,
     password   : dataconf.password,
     database   : dataconf.database
})

exports.query = function(sql,callback){
   pool.getConnection( function(err,connection){
       if( err ){
           if( callback ) callback(err);
       }else{
           connection.query( sql,function( err, result ){
               if( result ){
                   if( callback ) callback( err,result );
                   connection.release();
               }
           })
       }
   })
}











